﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.DirectX.DirectSound;
using System.Diagnostics;
using SKYPE4COMLib;

namespace SoundServant.Controls
{
    /// <summary>
    /// Interaction logic for ConfigControl.xaml
    /// </summary>
    public partial class LogsControl : UserControl
    {
        private delegate void VoidInvokeHandler();

        Sharing sharing;

        public LogsControl(Sharing _sharing)
        {

            InitializeComponent();

            sharing = _sharing;

            

            LogViewerTextBox.AppendText("SOUND SERVANT " + SS.VersionName);
            Logs.NewMessage += new Logs.NewMessageEventHandler(Logs_NewMessage);
        }

        void sharing_CallsUpdated()
        {
            if (sharing.Calls.Count > 0)
            {
                SkypeCallsTextBox.Text = "Calls (" + sharing.Calls.Count + "):";
                foreach (Call call in sharing.Calls)
                {
                    SkypeCallsTextBox.AppendText("\r\n" + call.PartnerHandle + " " + call.Id + " " + SoundServant.Sharing.ResolveCallStatus(call.Status));
                }
            }
            else
            {
                SkypeCallsTextBox.Text = "No Active Calls";
            }
        }

        void sharing_Message(SKYPE4COMLib.Command command)
        {
            SkypeConsoleTextBox.AppendText("\r\n" + command.Reply);
            SkypeConsoleTextBox.ScrollToEnd();
        }

        void Logs_NewMessage(LogMessage logMessage)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Send, (VoidInvokeHandler)delegate()
            {
                LogViewerTextBox.AppendText("\r\n" + logMessage.Time.ToString("dd.MM.yy HH:mm:ss") + " [" + logMessage.Title + "] " + logMessage.Message);
                if (!LogViewerTextBox.IsFocused)
                    LogViewerTextBox.ScrollToEnd();
            });
        }


        private void DisplaySkypeAPICheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (sharing != null)
            {
                sharing.Message += new Sharing.MessageEventHandler(sharing_Message);
                sharing.CallsUpdated += new Sharing.VoidEventHandler(sharing_CallsUpdated);
            }
        }

        private void DisplaySkypeAPICheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (sharing != null)
            {
                sharing.Message -= sharing_Message;
                sharing.CallsUpdated -= sharing_CallsUpdated;
            }
        }


    }
}
